package com.seu.clab.farm.service.impl;

import com.seu.clab.farm.model.dao.entity.Account;
import com.seu.clab.farm.model.dao.mapper.AccountMapper;
import com.seu.clab.farm.model.dao.mapper.PhoneMapper;
import com.seu.clab.farm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.seu.clab.farm.common.CONST;
import com.seu.clab.farm.exception.BizException;
import com.seu.clab.farm.model.dto.post.Register;
import com.seu.clab.farm.util.ConstantUtil;


import java.util.Date;
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;

    @Autowired
    PhoneMapper phoneMapper;

    @Override
    public void changeAccountInfo(Account account) {
        accountMapper.updateByPrimaryKeySelective(account);
    }

    @Override
    public void changePhone(String phone, String accountid) {
        phoneMapper.updatePhone(phone);
    }

    @Override
    public String getPhone() {
        return phoneMapper.getPhone();
    }

    @Override
    public void registerUser(Register register,String accountId){
        Account account = new Account();
        account.setAccountId(accountId);
        if(accountMapper.selectByPrimaryKey(accountId).getName()!=null){
            throw new BizException(ConstantUtil.BizExceptionCause.HAVE_REGISTER);
        }
        if(register.getAvatar()==null||register.getAvatar().equals("")){
            account.setAvatar(CONST.avatar);
        }else {
            account.setAvatar(register.getAvatar());
        }
        account.setName(register.getName());
        account.setPhone(register.getPhone());
        account.setEmail(register.getEmail());
        account.setStartTime(new Date());
        accountMapper.updateByPrimaryKeySelective(account);
    }
}

